package com.xgggh.redpack.ui.my;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProviders;

import com.f2prateek.rx.preferences2.RxSharedPreferences;
import com.jakewharton.rxbinding3.view.RxView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseActivity;
import com.xgggh.redpack.databinding.ActivityMyWalletBinding;
import com.xgggh.redpack.domain.wallet.Wallet;
import com.xgggh.redpack.ui.pay.PayActivity;

import javax.inject.Inject;

import dagger.android.AndroidInjection;
import io.reactivex.functions.Consumer;
import io.reactivex.subscribers.DisposableSubscriber;
import kotlin.Unit;
import model.Rsp;
import viewmodel.OauthViewModel;
import viewmodel.ViewModelFactory;
import viewmodel.WalletViewModel;


public class MyWalletActivity extends BaseActivity {
    ActivityMyWalletBinding mBinding;
    @Inject
    ViewModelFactory factory;
    @Inject
    RxSharedPreferences sharedPreferences;
    WalletViewModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        mBinding= DataBindingUtil. setContentView(this,R.layout.activity_my_wallet);
        model= ViewModelProviders.of(this,factory).get(WalletViewModel.class);

        setSupportActionBar(mBinding.toolbar.toolbar);
        mBinding.contentMyWalletStatistics.twinklingRefreshLayout.setPureScrollModeOn();
        RxView.clicks(mBinding.contentMyWalletStatistics.charge)
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        startActivity(new Intent(MyWalletActivity.this, PayActivity.class));
                    }
                });

        getWallet();
    }



    private void getWallet(){
        model.getWallet()
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new DisposableSubscriber<Rsp<Wallet>>() {
                    @Override
                    public void onNext(Rsp<Wallet> walletRsp) {
                        updateUI(walletRsp.data);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void updateUI(Wallet wallet){
        if (wallet==null){
            return;
        }
        String amount=wallet.getAmount()+"";
        mBinding.contentMyWalletStatistics.amount.setText(amount);
    }




}
